<template>
  <div class="container" @click.stop="showLanguageHandel">
    <div class="page_content container-tt">
      <header class="flex_between">
        <div class="headLogo">
          <nuxt-link to="/de">
            <div class="flex_">
              <img
                src="/static/logo.svg"
                class="img"
                alt="logo"
                width="34"
                height="34"
              />
              <span class="projectName">SavingTik</span>
            </div>
          </nuxt-link>
        </div>
        <div class="headTabs flex_">
          <!-- <div class="tItem">
            <nuxt-link to="/de">
              <div @click="goToTop('video')">TikTok Downloader</div>
            </nuxt-link>
          </div> -->
          <div class="tItem tItemBlod bor">
            <nuxt-link to="/de/download-tiktok-mp3">
              <div @click="goToTop('mp3')">TikTok MP3 herunterladen</div>
            </nuxt-link>
          </div>
          <nuxt-link to="/de/download-tiktok-story">
            <div @click="goToTop('story')" class="tItem">
              TikTok Story herunterladen
            </div>
          </nuxt-link>
          <language ref="languageRef"></language>
        </div>
        <div class="drawer" @click="showAppRightHandel">
          <img src="/static/menu.png" class="img" alt="menu" />
        </div>
      </header>
      <div class="banner_box">
        <div class="banner flex_ccenter">
          <h1 class="h1">Online TikTok MP3-Downloader</h1>
          <h2 class="h2">
            - Downloaden und konvertieren Sie TikTok-Videos in MP3-Musik. -
          </h2>
        </div>
      </div>
      <div class="downloadInput flex_center">
        <div class="inputBox flex_between">
          <input
            type="text"
            v-model="input"
            @keydown="sendDownload($event)"
            @input="handleInput"
            @paste="handleInput"
            class="ipt"
            placeholder="Fügen Sie hier den TikTok-Link ein"
            :disabled="isHttpLoading"
          />
          <div
            class="clearx flex_ccenter"
            v-if="input != ''"
            @click.stop="input = ''"
          >
            <img src="/static/clear.png" class="img" alt="" />
          </div>
          <div
            class="downBtn shou flex_ccenter"
            :class="isHttpLoading ? 'isHttpLoading' : ''"
            @click.stop="Download"
          >
            <div class="downImg">
              <img src="/static/download.webp" class="img" alt="download" />
            </div>
            <div class="iptbtn">Download</div>
          </div>
          <div class="posi shou" v-if="input != ''" @click.stop="input = ''">
            <img src="/static/clear.png" class="img" alt="" />
          </div>
        </div>
      </div>
      <div class="downStorySuccess" v-if="downStorySuccess">
        <div class="storyVideoInfo flex_between">
          <div class="downLeft flex_">
            <img :src="downData.avatar" class="img" alt="" />
            <div class="flex_column info">
              <div class="text1">
                {{ downData.title }}
              </div>
              <div class="text2">{{ downData.nickname }}</div>
            </div>
          </div>
          <div class="downBtn flex_ccenter shou" @click="allIn">
            Download All
          </div>
        </div>
        <div class="storyImageList flex_between">
          <div class="item" :key="k" v-for="(item, k) in downData.images">
            <client-only>
              <el-image
                style="width: 100%; height: 100%"
                fit="cover"
                :src="item"
                :preview-src-list="downData.images"
              >
              </el-image>
            </client-only>
            <div class="overlay">
              <div
                class="posi shou flex_center"
                @click="handleDownload(item, 'SavingTik-Image', 'image')"
              >
                <img
                  src="/static/download.webp"
                  class="xiazai"
                  alt="download"
                />
                <div>Download Photo</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="downSuccess flex_center" v-if="downSuccess">
        <div class="success_box">
          <div class="_video flex_between">
            <div class="bigImg flex_ccenter">
              <img :src="downData.cover" class="img" alt="" />
            </div>
            <div class="downInfo flex_column">
              <div class="info_1 flex_between">
                <div class="tit flex_">
                  <div class="timg">
                    <img
                      src="/static/blue.webp"
                      class="img"
                      alt="blue square"
                    />
                  </div>
                  <div>{{ downData.nickname }}</div>
                </div>
                <div class="times flex_">
                  <img src="/static/time.png" class="img" alt="" />
                  {{ duration }}
                </div>
              </div>
              <div class="info_2">
                {{ downData.title }}
              </div>
              <div class="info_3">
                <div class="item flex_between">
                  <div class="downLeft flex_column">
                    <div class="text1">Ohne Wasserzeichen</div>
                    <div class="text2">{{ WithoutVideoSize }}</div>
                  </div>
                  <div
                    class="downBtn flex_ccenter shou"
                    @click="
                      handleDownload(
                        downData.video_url,
                        'SavingTik-Without Watermark',
                        'video'
                      )
                    "
                  >
                    Download
                  </div>
                </div>
                <!-- <div class="item flex_between">
                  <div class="downLeft flex_column">
                    <div class="text1">Video Download</div>
                    <div class="text2">{{VideoSize }}</div>
                  </div>
                  <div class="downBtn flex_ccenter shou">Download</div>
                </div> -->
                <div class="item flex_between">
                  <div class="downLeft">
                    <div class="text1">Download MP3</div>
                  </div>
                  <div
                    class="downBtn flex_ccenter shou"
                    @click="
                      handleDownload(
                        downData.mp3_url,
                        'SavingTik-Download MP3',
                        'mp3'
                      )
                    "
                  >
                    Download
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="downStorySuccess_app" v-if="downStorySuccess">
        <div class="storyVideoInfo flex_between">
          <div class="downLeft flex_">
            <img :src="downData.avatar" class="img" alt="" />
            <div class="flex_column info">
              <div class="text1">
                {{ downData.title }}
              </div>
              <div class="text2">{{ downData.nickname }}</div>
            </div>
          </div>
        </div>
        <div class="allDown flex_ccenter" @click="allIn">
          Download All Photo
        </div>
        <div class="storyImageList flex_between">
          <div
            class="item"
            @click.stop="previewImage(item, k)"
            :key="k"
            v-for="(item, k) in downData.images"
          >
            <img :src="item" class="img" alt="" />
            <div class="overlay">
              <div
                class="posi shou flex_center"
                @click.stop="handleDownload(item, 'SavingTik-Image', 'image')"
              >
                <img src="/static/appxiazai.png" class="xiazai" alt="" />
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="downSuccess_app flex_center" v-if="downSuccess">
        <div class="success_box">
          <div class="_video flex_between">
            <div class="bigImg flex_ccenter">
              <img :src="downData.cover" class="img" alt="" />
            </div>
            <div class="downInfo flex_column">
              <div class="info_1">
                <div class="tit flex_">
                  <div class="timg">
                    <img src="/static/point.png" class="img" alt="" />
                  </div>
                  <div class="uname">{{ downData.title }}</div>
                </div>
              </div>
              <div class="info_2">{{ downData.nickname }}</div>
              <div class="times flex_between">
                <div></div>
                <div>Time — — {{ duration }}</div>
              </div>
            </div>
          </div>
          <div class="info_3">
            <div class="item flex_between">
              <div class="downLeft flex_column">
                <div class="text1">Ohne Wasserzeichen</div>
                <div class="text2">{{ WithoutVideoSize }}</div>
              </div>
              <div
                class="downBtn flex_ccenter shou"
                @click="
                  handleDownload(
                    downData.video_url,
                    'SavingTik-Without Watermark',
                    'video'
                  )
                "
              >
                Download
              </div>
            </div>

            <div class="item flex_between">
              <div class="downLeft">
                <div class="text1">Download MP3</div>
              </div>
              <div
                class="downBtn flex_ccenter shou"
                @click="
                  handleDownload(
                    downData.mp3_url,
                    'SavingTik-Download MP3',
                    'mp3'
                  )
                "
              >
                Download
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="loading" v-if="isHttpLoading">
        <div class="loading-dots flex_center">
          <div class="dot"></div>
          <div class="dot"></div>
          <div class="dot"></div>
        </div>
        <div class="tips flex_center">
          <div class="tip">
            <img src="/static/tip.png" class="img" alt="prompt" />
          </div>
          <div>Daten werden abgerufen, bitte warten Sie einige Sekunden!</div>
        </div>
      </div>
      <div class="error flex_ccenter" v-if="httpError">
        <div class="hono flex_">
          <img src="/static/tishi.png" class="img" alt="prompt" />
          <div>oh nein~</div>
        </div>
        <div class="errMsg">
          Der aktuelle Link ist falsch; bitte kopieren Sie den richtigen Link.
        </div>
      </div>
      <div class="declare">
        <div class="tit flex_center">
          <div class="timg">
            <img src="/static/blue.webp" class="img" alt="blue square" />
          </div>
          <h3 class="h3">TikTok MP3-Downloader Online</h3>
        </div>
        <div class="text_content">
          SaveTik ist ein TikTok MP3-Downloader, der es Benutzern ermöglicht,
          TikTok-Videos in MP3-Dateien herunterzuladen und zu konvertieren.
          Dieser Service ist einfach zu bedienen, sicher und zugänglich. Ihre
          Lieblings-MP3-Songs oder -Videos könnten mit ein paar Mausklicks
          erhalten werden. Das Hochladen der konvertierten Dateien in Ihre
          DropBox wird ebenfalls unterstützt. Es steht Ihnen frei, so viel
          herunterzuladen, wie Sie möchten. Die Installation und Registrierung
          von Software ist nicht notwendig. Unser Server unterstützt fast jede
          Art von System und Gerät, einschließlich Windows, Mac, Linux, Android,
          iPhone und iPad. Außerdem können Benutzer TikTok-Videos herunterladen
          und konvertieren, ohne sich Sorgen über das Durchsickern persönlicher
          Informationen zu machen, da kein Login erforderlich ist.
        </div>
      </div>
      <div class="next_content flex_ccenter">
        <div class="timeline_title flex_">
          <div class="timg">
            <img src="/static/blue.webp" class="img" alt="blue square" />
          </div>
          <h3 class="h3">Wie man den MP3-Downloader auf TikTok benutzt</h3>
        </div>
        <div class="timeline_content">
          <div class="section">
            <div class="section-upper flex_">
              <div class="section-left">
                <div class="current">1</div>
              </div>
              <div class="section-right">Finden Sie den MP3 TikTok Video-Link.</div>
            </div>
            <div class="section-lower flex_">
              <div class="Group flex_center">
                <div class="section-left">
                  <div class="Group1-bgImg"></div>
                </div>
              </div>
              <div class="section-right">
                Starten Sie die TikTok-App oder Website, wählen Sie das gewünschte Lied oder Video aus und kopieren Sie die URL.
              </div>
            </div>
          </div>
          <div class="section">
            <div class="section-upper flex_">
              <div class="section-left">
                <div class="current">2</div>
              </div>
              <div class="section-right">Kopieren Sie den MP3-Video-Link.</div>
            </div>
            <div class="section-lower flex_">
              <div class="Group flex_center">
                <div class="section-left">
                  <div class="Group2-bgImg"></div>
                </div>
              </div>
              <div class="section-right">
               Nachdem Sie die MP3-Video-URL in das Eingabefeld eingefügt haben, drücken Sie die "Suchen"-Taste.
              </div>
            </div>
          </div>
          <div class="section">
            <div class="section-upper flex_">
              <div class="section-left">
                <div class="current">3</div>
              </div>
              <div class="section-right">Laden Sie die MP3 herunter.</div>
            </div>
            <div class="section-lower flex_">
              <div class="Group flex_center">
                <div class="section-left">
                  <div class="Group4-bgImg"></div>
                </div>
              </div>
              <div class="section-right">
                Wählen Sie MP3 aus und tippen Sie dann auf die "Download"-Schaltfläche, um die Musik zu speichern.
              </div>
            </div>
          </div>
        </div>
        <div class="timeline_content_app">
          <div class="section">
            <div class="section-upper flex_">
              <div class="section-left">
                <div class="red-dot"></div>
              </div>
              <div class="section-right">
                1. Finden Sie den MP3 TikTok Video-Link.
              </div>
            </div>
            <div class="section-lower flex_">
              <div class="section-right">
                Starten Sie die TikTok-App oder Website, wählen Sie das gewünschte Lied oder Video aus und kopieren Sie die URL.
              </div>
            </div>
          </div>
          <div class="section">
            <div class="section-upper flex_">
              <div class="section-left">
                <div class="red-dot"></div>
              </div>
              <div class="section-right">2. Kopieren Sie den MP3-Video-Link.</div>
            </div>
            <div class="section-lower flex_">
              <div class="section-right">
               Laden Sie die MP3 herunter.
              </div>
            </div>
          </div>
          <div class="section">
            <div class="section-upper flex_">
              <div class="section-left">
                <div class="red-dot"></div>
              </div>
              <div class="section-right">3. Laden Sie die MP3 herunter.</div>
            </div>
            <div class="section-lower flex_">
              <div class="section-right">
                Wählen Sie MP3 aus und tippen Sie dann auf die "Download"-Schaltfläche, um die Musik zu speichern.
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="note" v-if="false">
        <span class="nt">Note:</span> SavingTik is not a tool of TikTok, we have
        no relationship with TikTok and the company ByteDance. We only support
        Tiktok users to download our videos on Tiktok without watermark easily
        and quickly. Thank you!
      </div>

      <div class="faqs">
        <div class="flex_center">
          <h3 class="faq_title">- Häufig gestellte Fragen -</h3>
        </div>
        <div class="faq_content">
          <div id="faq" itemscope="" itemtype="https://schema.org/FAQPage">
            <div
              itemprop="mainEntity"
              itemscope=""
              itemtype="https://schema.org/Question"
              class="faq_item shou"
              @click="changeFaqItemOpen(item, k)"
              :key="k"
              v-for="(item, k) in list"
            >
              <button style="width: 100%">
                <div class="itemTitle flex_between">
                  <h5 itemprop="name" class="tit">{{ item.title }}</h5>
                  <div class="imgs">
                    <div v-if="!item.isOpen" class="opentriangle"></div>
                    <div v-else class="closetriangle"></div>
                  </div>
                </div>
              </button>
              <div
                itemprop="acceptedAnswer"
                itemscope=""
                itemtype="https://schema.org/Answer"
              >
                <div
                  itemprop="text"
                  class="faq_text"
                  :class="item.isOpen ? '' : 'isBlock'"
                >
                  <template v-if="item.textContent">
                    {{ item.textContent }}
                  </template>
                  <ul v-else>
                    <li :key="i" v-for="(item1, i) in item.text">
                      {{ item1 }}
                    </li>
                  </ul>
                  <div v-if="item.b">
                    <b>{{ item.b.btit }}</b>
                    <div style="word-wrap: break-word">
                      {{ item.b.burl }}
                    </div>
                    <div>{{ item.b.btext }}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="footer">
        <pc-footer></pc-footer>
      </div>
    </div>
    <div class="page_bottom flex_center">
      ©SavingTik.Com 2023 All Rights Reserved.
    </div>
    <article-content></article-content>
    <div class="app_page_bottom">
      <app-footer></app-footer>
    </div>
    <video src="" id="video" autoplay muted v-show="false"></video>
    <div class="dloading" v-if="dloading">
      <div class="loader"></div>
    </div>
    <van-popupapp ref="popupAppRef"></van-popupapp>
  </div>
</template>
  
  <script>
import dataList from "@/static/faqsMp3.js";
import { handleClick } from "@/plugins/Firebase.js";
import CryptoJS from "crypto-js";
import { ImagePreview } from "vant";
import language from "@/components/language.vue";
import VanPopupapp from "@/components/vanPopupapp.vue";
import PcFooter from "@/components/pcFooter.vue";
import AppFooter from "@/components/appFooter.vue";
import ArticleContent from '~/components/articleContent.vue';

export default {
  components: { language, VanPopupapp, PcFooter, AppFooter, ArticleContent },
  head: {
    htmlAttrs: {
      lang: "de",
    },
    bodyAttrs: {
      "data-lang": "de",
    },
    title: "TikTok MP3 herunterladen mit kostenlosem Tik Tok MP3-Downloader online",
    meta: [
      { charset: "utf-8" },
      { property: "og:locale", content: "de" },
      {
        name: "viewport",
        content:
          "width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no",
      },
      { name: "robots", content: "index, follow" },
      { name: "revisit-after", content: "1 days" },
      {
        name: "description",
        content:
          "SavingTik - Bester TikTok MP3-Downloader. Konvertieren Sie TikTok-Videos kostenlos in hochwertige MP3-Audiodateien. Keine Registrierung, keine Grenzen.",
      },
      {
        itemprop: "name",
        content: "TikTok MP3 herunterladen mit kostenlosem Tik Tok MP3-Downloader online",
      },
      { name: "author", content: "SavingTik" },
      { name: "msvalidate.01", content: "" },
      { itemprop: "image", content: "/static/logo/savingtik-thumb.webp" },
      { name: "twitter:card", content: "summary" },
      {
        name: "twitter:title",
        content: "TikTok MP3 herunterladen mit kostenlosem Tik Tok MP3-Downloader online",
      },
      {
        name: "twitter:description",
        content:
          "SavingTik - Bester TikTok MP3-Downloader. Konvertieren Sie TikTok-Videos kostenlos in hochwertige MP3-Audiodateien. Keine Registrierung, keine Grenzen.",
      },
      {
        name: "twitter:image:src",
        content: "/static/logo/savingtik-thumb.webp",
      },
      { name: "twitter:site", content: "SavingTik" },
      {
        property: "og:title",
        content: "TikTok MP3 herunterladen mit kostenlosem Tik Tok MP3-Downloader online",
      },
      { property: "og:type", content: "article" },
      { property: "og:image", content: "/static/logo/savingtik-thumb.webp" },
      {
        property: "og:description",
        content:
          "SavingTik - Bester TikTok MP3-Downloader. Konvertieren Sie TikTok-Videos kostenlos in hochwertige MP3-Audiodateien. Keine Registrierung, keine Grenzen.",
      },
      { property: "og:site_name", content: "SavingTik" },
    ],
    link: [
      // Favicons
      {
        rel: "shortcut icon",
        href: "/static/logo/favicon.png",
        sizes: "512x512",
      },
      {
        rel: "apple-touch-icon",
        href: "/static/logo/favicon.png",
        sizes: "192x192",
      },
      //Apple Touch Icons

      //multilingual
      {
        rel: "alternate",
        hreflang: "x-default",
        href: "https://savingtik.com/de/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "en",
        href: "https://savingtik.com/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "en-in",
        href: "https://savingtik.com/in/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "vi",
        href: "https://savingtik.com/vn/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "tr",
        href: "https://savingtik.com/tr/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "id",
        href: "https://savingtik.com/id/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "fr",
        href: "https://savingtik.com/fr/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "pt",
        href: "https://savingtik.com/pt/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "ru",
        href: "https://savingtik.com/ru/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "es",
        href: "https://savingtik.com/es/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "ms",
        href: "https://savingtik.com/ms/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "ko",
        href: "https://savingtik.com/ko/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "ja",
        href: "https://savingtik.com/ja/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "jv",
        href: "https://savingtik.com/jv/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "cs",
        href: "https://savingtik.com/cs/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "de",
        href: "https://savingtik.com/de/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "it",
        href: "https://savingtik.com/it/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "pl",
        href: "https://savingtik.com/pl/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "hu",
        href: "https://savingtik.com/hu/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "nl",
        href: "https://savingtik.com/nl/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "ro",
        href: "https://savingtik.com/ro/download-tiktok-mp3",
      },
      {
        rel: "alternate",
        hreflang: "el",
        href: "https://savingtik.com/el/download-tiktok-mp3",
      },
      {
        rel: "canonical",
        href: "https://savingtik.com/de/download-tiktok-mp3",
      },
    ],
  },
  name: "download-MP3",
  data() {
    return {
      list: dataList.listDe,
      input: "",
      dloading: false,
      showAppRight: false,
      showLanguage: false,
      Language: "en_GB",
      isHttpLoading: false,
      downSuccess: false,
      downStorySuccess: false,
      httpError: false,
      downData: {},
      WithoutVideoSize: "",
      duration: "",
      VideoSize: "",
    };
  },
  mounted() {
    this.debouncedDownInput = this.debounce(this.downInput, 1000);
  },
  methods: {
    showAppRightHandel() {
      this.$refs.popupAppRef.open();
    },
    showLanguageHandel() {
      this.$refs.languageRef.close();
    },
    goToTop(type) {
      // 切换页面 action_page_switch
      handleClick("action_page_switch", {
        site: type,
        from: "top",
      });
    },
    goToTools(type) {
      // 切换页面 action_page_switch
      handleClick("action_page_switch", {
        site: type,
        from: "tools",
      });
    },
    debounce(fn, delay) {
      let timer;
      return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(() => {
          fn.apply(context, args);
        }, delay);
      };
    },
    downInput(e) {
      // 粘贴链接到输入框 action_tt_paste
      handleClick("action_tt_paste", {
        islink: e.target.value,
        from: "mp3",
      });
    },
    handleInput(e) {
      this.debouncedDownInput(e);
    },
    previewImage(item, k) {
      ImagePreview({
        images: this.downData.images,
        startPosition: k,
      });
    },
    sendDownload(event) {
      if (event.keyCode === 13 && !this.isHttpLoading) {
        this.Download();
        event.preventDefault();
        return false;
      }
    },
    async allIn() {
      // 设定 URL 数组
      //点击开始下载按钮 action_tt_download_start
      handleClick("action_tt_download_start", {
        site: "all",
        from: "mp3",
      });
      // 通过 map 返回一个 Promise 数组
      let promises = this.downData.images.map((url) =>
        this.handleDownloadImages(url)
      );

      // 等待所有 Promise 完成
      await Promise.all(promises);
      this.dloading = false;
    },
    async handleDownloadImages(src) {
      try {
        this.dloading = true;
        const response = await fetch(src);
        if (!response.ok) throw new Error("网络响应不好");
        const data = await response.blob();
        let url = window.URL.createObjectURL(data);
        let link = document.createElement("a");
        link.href = url;
        link.download = "SavingTik-" + new Date().getTime();
        document.body.appendChild(link);
        link.click();
        link.remove();
        // //开始下载 tt_download_start
        handleClick("tt_download_start", {
          site: "all",
          from: "mp3",
        });
      } catch (error) {
        this.dloading = false;
        console.error("捕获到错误: " + error);
      }
    },
    async handleDownload(src, fileName, type) {
      if (type == "image") {
        //点击开始下载 action_tt_download_start
        handleClick("action_tt_download_start", {
          site: "story",
          from: "mp3",
        });
      } else if (type == "video") {
        //点击开始下载 tt_download_start
        handleClick("action_tt_download_start", {
          site: "video",
          from: "mp3",
        });
      } else {
        //点击开始下载 tt_download_start
        handleClick("action_tt_download_start", {
          site: "mp3",
          from: "mp3",
        });
      }
      try {
        this.dloading = true;
        const response = await fetch(src);
        if (!response.ok) throw new Error("网络响应不好");
        const data = await response.blob();
        let url = window.URL.createObjectURL(data);
        let link = document.createElement("a");
        link.href = url;
        link.download = "SavingTik-" + new Date().getTime();
        document.body.appendChild(link);
        link.click();
        link.remove();
        this.dloading = false;
        if (type == "image") {
          //开始下载 tt_download_start
          handleClick("tt_download_start", {
            site: "story",
            from: "mp3",
          });
        } else if (type == "video") {
          //开始下载 tt_download_start
          handleClick("tt_download_start", {
            site: "video",
            from: "mp3",
          });
        } else {
          //开始下载 tt_download_start
          handleClick("tt_download_start", {
            site: "mp3",
            from: "mp3",
          });
        }
      } catch (error) {
        this.dloading = false;
        console.error("捕获到错误: " + error);
      }
    },
    encryptUrl(url, key, iv) {
      // DES 加密
      let encrypted = CryptoJS.DES.encrypt(
        `m=url&url=${encodeURIComponent(url)}`,
        CryptoJS.enc.Utf8.parse(key),
        {
          iv: CryptoJS.enc.Utf8.parse(iv),
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.Pkcs7,
        }
      );
      // 转 Base64
      let base64String = encrypted.toString();
      // URL 编码
      let result = encodeURIComponent(base64String);
      // 最终结果
      return `https://savingtik.com/tt.php?v=${result}`;
    },
    decryptUrl(encryptedUrl, key, iv) {
      // URL 解码
      const decodedUrl = decodeURIComponent(encryptedUrl);

      // Base64 解码
      const parsedWordArray = CryptoJS.enc.Base64.parse(decodedUrl);

      // DES解密
      const decrypted = CryptoJS.DES.decrypt(
        {
          ciphertext: parsedWordArray,
        },
        CryptoJS.enc.Utf8.parse(key),
        {
          iv: CryptoJS.enc.Utf8.parse(iv),
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.Pkcs7,
        }
      );
      return decrypted.toString(CryptoJS.enc.Utf8);
    },
    async Download() {
      // 早些返回以避免嵌套
      if (this.isHttpLoading || this.input === "") return;
      this.downData = {};
      this.httpError = false;
      this.downStorySuccess = false;
      this.downSuccess = false;
      const tiktokUrlPattern = new RegExp("^http(s|)://.*tiktok.com.*/.*$");
      if (!tiktokUrlPattern.test(this.input)) {
        //点击解析下载按钮 action_tt_detect_bottom
        handleClick("action_tt_detect_bottom", {
          site: "other",
          noislink: this.input,
          from: "mp3",
        });
        this.httpError = true;
        this.isHttpLoading = false;
        return;
      }
      //点击解析下载按钮 action_tt_detect_bottom
      handleClick("action_tt_detect_bottom", {
        site: "tt",
        from: "mp3",
      });
      //开始解析  tt_start_resolve
      handleClick("tt_start_resolve", {
        islink: this.input,
        from: "mp3",
      });
      this.isHttpLoading = true;

      try {
        let result = this.encryptUrl(this.input, process.env.DES_KEY, process.env.DES_IV);
        const response = await fetch(result, {
          method: "GET",
          headers: { "Content-Type": "application/json" },
        });
        let encryptedResponse = await response.text();
        let decryptedResponse = this.decryptUrl(
          encryptedResponse,
          process.env.DES_KEY,
          process.env.DES_IV
        ); // 使用你的解密函数
        let data = JSON.parse(decryptedResponse).data;
        // 检查数据是否存在
        if (!data) {
          this.httpError = true;
          this.isHttpLoading = false;
          return;
        }

        if (!data.images) {
          await this.fetchVideoAttributes(data);
          //解析成功  tt_resolve_success
          handleClick("tt_resolve_success", {
            site: "video",
            from: "mp3",
          });
        } else {
          this.downData = data;
          this.downStorySuccess = true;
          this.isHttpLoading = false;
          //解析成功  tt_resolve_success
          handleClick("tt_resolve_success", {
            site: "story",
            from: "mp3",
          });
        }
      } catch (error) {
        //解析失败  tt_resolve_fail
        handleClick("tt_resolve_fail", {
          reason: "Failed to fetch",
          islink: this.input,
          from: "mp3",
        });
        console.error(error);
        this.httpError = true;
        this.isHttpLoading = false;
      }
    },

    async fetchVideoAttributes(data) {
      try {
        const videoBlob = await fetch(data.video_url).then((res) => res.blob());
        this.WithoutVideoSize = `${(videoBlob.size / (1024 * 1024)).toFixed(
          2
        )} MB`;
        // const videoBlob1 = await fetch(this.input).then((res) => res.blob());
        // this.VideoSize  = `${(videoBlob1.size / (1024 * 1024)).toFixed(
        //   2
        // )} MB`;
        const video = document.getElementById("video");
        video.setAttribute("src", data.video_url);
        video.crossOrigin = "anonymous";
        video.innerHTML = `<source src=${data.video_url} type="audio/mp4" >`;

        await new Promise((resolve) =>
          video.addEventListener("canplaythrough", () => {
            this.duration = this.formatSeconds(video.duration);
            resolve();
          })
        );

        await new Promise((resolve) => {
          const image = new Image();
          image.onload = resolve;
          image.src = data.cover;
        });

        this.downData = data;
        this.isHttpLoading = false;
        this.downSuccess = true;
      } catch (error) {
        console.error(error);
        this.httpError = true;
        this.isHttpLoading = false;
      }
    },

    formatSeconds(seconds) {
      const hours = Math.floor(seconds / 3600);
      const minutes = Math.floor((seconds - hours * 3600) / 60);
      const secs = Math.floor(seconds % 60);

      // 使用模板字符串清理时间格式化
      const time = [
        hours ? (hours > 9 ? hours : `0${hours}`) : undefined,
        minutes > 9 ? minutes : `0${minutes}`,
        secs > 9 ? secs : `0${secs}`,
      ]
        .filter(Boolean)
        .join(":");

      return time;
    },
    changeFaqItemOpen(item, k) {
      item.isOpen = !item.isOpen;
    },
  },
};
</script>
  <style lang="scss" scoped>
@import "@/static/style/mp3.css";
.dloading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  z-index: 99999;

  .loader {
    width: 88px;
    height: 88px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .loader:before {
    content: "";
    width: 148px;
    height: 10px;
    background: #202a68;
    position: absolute;
    top: 116px;
    left: -27px;
    border-radius: 50%;
    animation: shadow324 0.5s linear infinite;
  }

  .loader:after {
    content: "";
    width: 100%;
    height: 100%;
    background: #202a68;
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 4px;
    animation: jump7456 0.5s linear infinite;
  }

  @keyframes jump7456 {
    15% {
      border-bottom-right-radius: 3px;
    }

    25% {
      transform: translateY(9px) rotate(22.5deg);
    }

    50% {
      transform: translateY(18px) scale(1, 0.9) rotate(45deg);
      border-bottom-right-radius: 40px;
    }

    75% {
      transform: translateY(9px) rotate(67.5deg);
    }

    100% {
      transform: translateY(0) rotate(90deg);
    }
  }

  @keyframes shadow324 {
    0%,
    100% {
      transform: scale(1, 1);
    }

    50% {
      transform: scale(1.2, 1);
    }
  }
}
.loading-dots {
  position: relative;
  z-index: 999;
  margin-top: 70px;
}

.dot {
  background-color: #83818c;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin: 0 10px;
  animation: loading-animation 1.5s infinite;
}

.dot:nth-child(2) {
  animation-delay: 0.5s;
}

.dot:nth-child(3) {
  animation-delay: 1s;
}

@keyframes loading-animation {
  0%,
  80%,
  100% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.5);
  }
}
</style>